import Vue from 'vue'
import Vuex from 'vuex'
import { GetSearchProduct } from '@/request/api'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 控制显示地址
    curAddress:{},
    // 控制popup的显隐
    popupShow: false,
    // popup中product的数组
    productArr: [],
    // 分类数组
    option3: [],
    hideLogin: false,
    isBug: false,
    // 显示添加商品
    showAddCart: false,

    // 选择加入购物车数量
    goodsNum: 1,

    // 购物车总数量
    cartNum: 0,
    isClose : false
  },
  mutations: {
    // 修改当前显示地址
    changeCurAddress(state, payload) {
      if(payload.url != '/user/address'){
        // console.log(payload.obj,payload.url)
        state.curAddress = payload.obj

      }
    },
    // 修改购物车总数量
    changeCartNum(state, payload) {
      state.cartNum = payload
    },
    // 修改搜索页面的true值
    changeClose(state, payload) {
      state.isClose = payload
    },
    // 修改加入购物车数量
    changeGoodsNum(state, payload) {
      state.goodsNum = payload
    },

    // 修改显示添加界面
    changeShowAddCart(state, payload) {
      state.showAddCart = payload
    },
    // 修改popup的显隐
    controlPopupShow(state, payload) {
      state.popupShow = payload;
    },
    // 修改产品数组
    changeProductArr(state, payload) {
      state.productArr = payload;
    },
    // 修改分类数组
    changeOption3(state, payload) {
      state.option3 = payload;
    },
    // 修改登录状态
    changehideLogin(state, payload) {
      state.hideLogin = payload;
    },
    changeBug(state, payload) {
      state.isBug = payload
    },
  },
  actions: {
    // 异步修改产品数组
    AsyncChangeProductArr({ commit, rootState }, payload) {
      // 请求数据
      GetSearchProduct({
        keyword: payload.keyword,
        categoryId: payload.categoryId,
        size: 239,
        count: 10,
        categoryId: payload.categoryId,
      }).then(res => {
        if (res.errno == 0) {
          // 得到产品数组
          commit("changeProductArr", res.data.goodsList);
          var arr = JSON.parse(JSON.stringify(res.data.filterCategory).replace(/name/g, 'text').replace(/id/g, 'value'));
          commit("changeOption3", arr);
          
        }
      })
    }
  },
  modules: {
  }
})
